﻿using AutoMapper;
using MediatR;
using S.Archives.Api.Application.Command;
using S.Archives.Domain.S.ArchivesModel;
using S.Archives.Infrstructre.InterFace;

namespace S.Archives.Api.Application.CommandHandlers
{
    public class SheepFileDeletedirectPLCommandHandler : IRequestHandler<SheepFileDeletedirectPLCommand, bool>
    {
        private readonly ISheepFileRepository sheepFileRepository;
        private readonly IMapper mapper;
        public SheepFileDeletedirectPLCommandHandler(ISheepFileRepository sheepFileRepository, IMapper mapper)
        {
            this.sheepFileRepository = sheepFileRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 批量直接删除羊只档案
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> Handle(SheepFileDeletedirectPLCommand request, CancellationToken cancellationToken)
        {
            var Ids = request.Ids.Split(',');
            List<SheepFileModel> sheepFileModels = new List<SheepFileModel>();
            foreach (var item in Ids)
            {
                int id=Convert.ToInt32(item);
                var model = sheepFileRepository.GetAll().FirstOrDefault(x=>x.SheepFileId== id);
                if (model != null)
                {
                    sheepFileModels.Add(model);
                }
                else
                {
                    return false;
                }
            }
            var data =await sheepFileRepository.RemoveRangeAsync(sheepFileModels);
            return data;


        }
    }
}
